package com.example.xiongan_app_yy.adapter;

import android.content.Context;
import android.graphics.Color;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.baidu.mapapi.search.sug.SuggestionResult;
import com.example.xiongan_app_yy.R;
import com.example.xiongan_app_yy.listener.OnMapItemClickListener;

import java.util.List;

/**
 * @ProjectName: android
 * @Package: com.example.xiongan_app_yy.adapter
 * @ClassName: MyAdapter
 * @Description: 描述：
 * @Author: 吴航
 * @CreateDate: 2019/10/25 14:04
 * @UpdateUser: 更新者：
 * @UpdateDate: 2019/10/25 14:04
 * @UpdateRemark: 更新说明：
 * @Version: 1.0
 */
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder>{


    private List<SuggestionResult.SuggestionInfo> mDatas;
    private  Context mContext;
    private int index=-1;

    /**
     * adapter 中item点击事件的监听对象.
     */
    private OnMapItemClickListener onClickListener;


    public void setOnMapItemClickListener(OnMapItemClickListener onClickListener) {
        this.onClickListener = onClickListener;
    }
    public void setIndex(int i) {
        this.index = i;
    }



    public MyAdapter(Context context,List<SuggestionResult.SuggestionInfo> data) {
        this.mDatas = data;
        this.mContext=context;
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        holder.title.setText(mDatas.get(position).key);
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //item 点击事件
                onClickListener.onClick(position);
                index=position;
                notifyDataSetChanged();
            }
        });
        if(index==position){
            holder.llLtemMap.setBackgroundColor(Color.parseColor("#00A5F1"));
        }
        else
        {
            holder.llLtemMap.setBackgroundColor(Color.parseColor("#ffffff"));
        }
    }

    @Override
    public int getItemCount() {
        return mDatas.size();
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(mContext).inflate(R.layout.item_map, parent, false);
        return new MyViewHolder(v);
    }


    public static class MyViewHolder extends RecyclerView.ViewHolder{
        public final TextView title;
        public final LinearLayout llLtemMap;
        public MyViewHolder(View v) {
            super(v);
            title = v.findViewById(R.id.tv_item);
            llLtemMap = v.findViewById(R.id.ll_item_map);
        }
    }
}
